﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using saveimagetodatabase;
using System.Data;
using System.Data.SqlClient;
namespace WcfServiceLibrary2
{
    [DataContract]
   public class Anh
    {
        [DataMember]
        public int Id{get;set;}
        [DataMember]
        public string FileName{get;set;}
        [DataMember]
        public byte[]ImageData{get;set;}
        public Anh(DataRow row)
        {
            this.Id = int.Parse(row["id"].ToString());
            this.FileName = row.IsNull("filename") ? string.Empty : row["filename"].ToString();
            this.ImageData=row.IsNull("image")?null:(byte[])row["image"];
        }
        public static void InsertAnh(Anh anh)
        {
          try
          {
                SqlQuery sql = new SqlQuery();
                List<SqlParameter>listPara=new List<SqlParameter>();
                listPara.Add(new SqlParameter("@filename",anh.FileName));
                listPara.Add(new SqlParameter("@image",anh
                  .ImageData));
                sql.ExcuteProceduteCommand("insertanh", listPara);
          }
          catch (System.Exception ex)
          {
          	throw ex;
          }
        }
        public static Anh GetImageById(int id)
        {
            try
            {
                SqlQuery sql = new SqlQuery();
                List<SqlParameter> listPara = new List<SqlParameter>();
                listPara.Add(new SqlParameter("@id", id));

                DataTable data = sql.ExcuteProcedute("GetanhById", listPara);
               
                   Anh anh = new Anh(data.Rows[0]);
                   return anh;

            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
        }
        public static List<Anh>GetAllAnh()
        {
            try
            {
                SqlQuery sql = new SqlQuery();
                

                DataTable data = sql.ExcuteProcedute("GetAllAnh", null);
                List<Anh> list = new List<Anh>();

               foreach (DataRow row in data.Rows)
               {
                   Anh anh = new Anh(row);
                   list.Add(anh);
               }
               return list;
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
        }
    }
}
